package org.fengling.readingplatform.novel.strategy;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fengling.readingplatform.model.entity.novel.Novel;
import org.fengling.readingplatform.novel.factory.HandlerSortStrategyFactory;
import org.fengling.readingplatform.novel.factory.SortFactory;
import org.fengling.readingplatform.novel.mapper.NovelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SortByLikeHandler implements HandlerSortStrategyFactory {

    @Autowired
    private NovelMapper novelMapper;

    @Override
    public PageInfo<Novel> findByPage(Integer page, Integer limit, String category) {
        //设置分页参数
        PageHelper.startPage(page, limit);
        //根据条件查询所有数据
        List<Novel> list = novelMapper.findByLike(category);
        //封装pageInfo对象
        PageInfo<Novel> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        SortFactory.register("SortByLike", this);
    }
}
